/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package admin.Controller;

import DTO.Category;
import DTO.Item;
import admin.BusinessLogic.BusinessLogic;
import admin_gui.Admin_EditItem;
import admin_gui.Admin_SearchItem;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author WEN
 */
public class ItemController {

    private BusinessLogic businesslogic;
    private JPanel mainPanel;
    Vector item;
    Vector wholevector;
    boolean[] itemItem;
    int deleteType = 0;             //to remember the selection type before deleting
    String searchSelection = "";
    Item objectItem;
    int ctid;

    public ItemController(JPanel MainPanel) {
        objectItem = new Item();
        try {
            //     System.out.println("in item controller1");
            businesslogic = new BusinessLogic();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(ItemController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(ItemController.class.getName()).log(Level.SEVERE, null, ex);
        }
        mainPanel = MainPanel;
        item = new Vector();
        itemItem = new boolean[6];
        itemItem[0] = true;
        for (int i = 1; i < 6; i++) {
            itemItem[i] = false;
        }
    }

    public void manageItem(JTable ResultTable, Vector v_head, String searchField,
            boolean[] itemItem, int ctid) {
   //     System.out.println("searchField:" + searchField);
        this.ctid = ctid;
        this.itemItem = itemItem;
        if (itemItem[0] == true || (searchField.length() == 0 && ctid == -1)) {
    //        System.out.println("inmanage");
            showItemTable(ResultTable, v_head);
        } else if (itemItem[1] == true) {
            showItemTableByName(searchField.trim(), ResultTable, v_head);
            deleteType = 1;
            searchSelection = searchField.trim();
        } else if (itemItem[2] == true) {
            showItemTableByUPC(searchField.trim(), ResultTable, v_head);
            deleteType = 2;
            searchSelection = searchField.trim();
        } else if (itemItem[3] == true) {
            showItemTableByMake(searchField.trim(), ResultTable, v_head);
            deleteType = 3;
            searchSelection = searchField.trim();
        } else if (itemItem[4] == true) {
            showItemTableByModel(searchField.trim(), ResultTable, v_head);
            deleteType = 4;
            searchSelection = searchField.trim();
        } else if (itemItem[5] == true) {
            showItemTableByCategory(searchField.trim(), ResultTable, v_head,
                    ctid);
            deleteType = 5;
            searchSelection = searchField.trim();
        } else {
            JOptionPane.showMessageDialog(null,
                    "Sorry! Can not find that information",
                    "Error", JOptionPane.ERROR_MESSAGE);
        }
    }

    private void showItemTable(JTable ResultTable, Vector v_head) {
        item = businesslogic.getItem();
        DefaultTableModel tm = new DefaultTableModel(item, v_head);
        ResultTable.setModel(tm);
        ((DefaultTableModel) ResultTable.getModel()).fireTableStructureChanged();
    }

    public boolean addItem(Item i) {
     //   System.out.println("in ItemController");
        boolean b = false;
       
            b = businesslogic.addItem(i);
        if (!b) {
            JOptionPane.showMessageDialog(null, "Sorry! UPC has existed.",
                    "Error", JOptionPane.ERROR_MESSAGE);
            return false;
        } else {
            Vector storeName = new Vector();
            storeName = businesslogic.getStore();
            String[] sname;
            sname = new String[storeName.size()];
            for (int j = 0; j < storeName.size(); j++) {
                sname[j] = ((Vector) storeName.elementAt(j)).get(0).toString();
                businesslogic.addItemToStore(sname[j], i.getUpc(), (int) 0);
            }
            return true;
        }
    }

    private void showItemTableByName(String name, JTable ResultTable,
            Vector v_head) {
        item.clear();
        item = businesslogic.getItemsByName(name);
        DefaultTableModel tm = new DefaultTableModel(item, v_head);
        ResultTable.setModel(tm);
        ((DefaultTableModel) ResultTable.getModel()).fireTableStructureChanged();
    }

    private void showItemTableByUPC(String UPC, JTable ResultTable,
            Vector v_head) {
        item.clear();
        item = businesslogic.getItemByUPC(UPC);
        DefaultTableModel tm = new DefaultTableModel(item, v_head);
        ResultTable.setModel(tm);
        ((DefaultTableModel) ResultTable.getModel()).fireTableStructureChanged();
    }

    private void showItemTableByMake(String make, JTable ResultTable,
            Vector v_head) {
        item.clear();
        item = businesslogic.getItemsByMake(make);
        DefaultTableModel tm = new DefaultTableModel(item, v_head);
        ResultTable.setModel(tm);
        ((DefaultTableModel) ResultTable.getModel()).fireTableStructureChanged();
    }

    private void showItemTableByModel(String model, JTable ResultTable,
            Vector v_head) {
        item.clear();
        item = businesslogic.getItemsByModel(model);
        DefaultTableModel tm = new DefaultTableModel(item, v_head);
        ResultTable.setModel(tm);
        ((DefaultTableModel) ResultTable.getModel()).fireTableStructureChanged();
    }

    private void showItemTableByCategory(String model, JTable ResultTable,
            Vector v_head, int ctid) {
        item.clear();
        item = businesslogic.getAdminItemsByCategory(ctid);
        DefaultTableModel tm = new DefaultTableModel(item, v_head);
        ResultTable.setModel(tm);
        ((DefaultTableModel) ResultTable.getModel()).fireTableStructureChanged();
    }

    public void deleteItem(String UPC, JTable ResultTable, Vector v_head) {
        if (businesslogic.deleteItem(UPC)) {
            if (deleteType == 0) {
                showItemTable(ResultTable, v_head);
            } else if (deleteType == 1) {
                showItemTableByName(searchSelection, ResultTable, v_head);
            } else if (deleteType == 2) {
                showItemTableByUPC(searchSelection, ResultTable, v_head);
            } else if (deleteType == 3) {
                showItemTableByMake(searchSelection, ResultTable, v_head);
            } else if (deleteType == 4) {
                showItemTableByModel(searchSelection, ResultTable, v_head);
            } else {
                showItemTableByCategory(searchSelection, ResultTable, v_head,
                        ctid);
            }
        }
    }

    public void showEditStorePanel(String UPC) {
        wholevector = businesslogic.getItemByUPC(UPC);
        Vector storeName = new Vector();
        storeName = businesslogic.getStore();
        Admin_EditItem editItemPane = new Admin_EditItem(this, wholevector,
                storeName);
        editItemPane.setBounds(1, 1, 762, 613);
        mainPanel.removeAll();
        mainPanel.add(editItemPane);
        mainPanel.validate();
        mainPanel.repaint();
    }

    public int getItemQtyInStore(String storeName, String upc) {
        return businesslogic.getItemQtyInStore(storeName, upc);
    }

    public void updateItem(String storeName, String upc, String name,
            String make,
            String model, String price, String retailPrice, String dis,
            String url, String qty, int ctid) {
        objectItem.setUPC(upc);
        objectItem.setDescription(dis);
        objectItem.setCostPrice(Double.parseDouble(price));
        objectItem.setImgURL(url);
        objectItem.setMake(make);
        objectItem.setModel(model);
        objectItem.setName(name);
        objectItem.setRetailPrice(Double.parseDouble(retailPrice));
        objectItem.setCategoryID(ctid);
        int quantity = Integer.parseInt(qty);
        businesslogic.updateItemInfo(upc, objectItem);
        businesslogic.updateItemQTYInStore(storeName, upc, quantity);
        JOptionPane.showMessageDialog(null,
                "The item information has been changed Sucessfully",
                "Message", JOptionPane.INFORMATION_MESSAGE);
    }

    public ArrayList<Category> getCategories() {
        ArrayList<Category> listCategory = new ArrayList<Category>();
        listCategory = businesslogic.getCategories();
        return listCategory;
    }

    public void goBackSerachItemPane() {
        Admin_SearchItem searchItemPane = new Admin_SearchItem(mainPanel);
        searchItemPane.setBounds(1, 1, 762, 613);
        mainPanel.removeAll();
        mainPanel.add(searchItemPane);
        mainPanel.validate();
        mainPanel.repaint();
    }
}
